package site.fsyj.util;

import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author fsyj on 2022/3/7
 */
@Slf4j
public class CookieUtil {

    public static Map<String,String> cookieToMap(String value) {
        Map<String, String> map = new HashMap<String, String>();
        value = value.replace(" ", "");
        if (value.contains(";")) {
            String[] values = value.split(";");
            for (String val : values) {
                String[] vals = val.split("=");
                map.put(vals[0], vals[1]);
            }
        } else {
            String[] values = value.split("=");
            map.put(values[0], values[1]);
        }
        return map;
    }

    public static void writeCookie(String cookie) {
        Properties properties = ResourceUtil.getConfigure("appConfig.properties");
        FileOutputStream outputStream = null;
        try {
            if (properties == null) {
                throw new IOException();
            }
            properties.setProperty("cookie",cookie);
            outputStream = new FileOutputStream(ResourceUtil.getPath("appConfig.properties"), false);
            properties.store(outputStream, "配置cookie");
        } catch (IOException e) {
            log.error("配置文件加载错误");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
